package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.exception.BusinessException;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.core.enumeration.ActivityStatus;
import com.ngc.mall.domain.ActivityDomain;
import com.ngc.mall.domain.CouponDomain;
import com.ngc.mall.mapper.ActivityMapper;
import com.ngc.mall.service.IActivityService;
import com.ngc.mall.util.CommonUtil;
import com.ngc.mall.vo.ActivityCouponVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动实现类
 *
 * @author: chenxiaolin
 * @date 2023/8/21 12:00
 **/
@Service
public class ActivityServiceImpl extends BaseServiceImpl<ActivityMapper, ActivityDomain> implements IActivityService {
    @Override
    public Long createActivity(ActivityDomain domain) {
        LocalDateTime now=LocalDateTime.now();
        Boolean isIn= CommonUtil.isInTimeLine(now,domain.getStartDate(),domain.getEndDate());
        if(isIn){
            domain.setStatus(ActivityStatus.DOING.getValue());
        }
        ///创建默认状态为【未开始】
        return super.create(domain);
    }

    @Override
    public Boolean updateById(Long id, ActivityDomain domain) {
        LocalDateTime now=LocalDateTime.now();
        ActivityDomain activityDomain=super.check(id);
        if(!now.isBefore(activityDomain.getStartDate())){
            ///不是【未开始】状态
            throw BusinessException.FAILURE;
        }
        return super.update(domain,Wrappers.<ActivityDomain>lambdaQuery().eq(ActivityDomain::getId, id));
    }


    @Override
    public ActivityCouponVO findById(Long id) {
        ActivityDomain domain = super.check(id);
        List<CouponDomain> couponDomainList = baseMapper.findCouponByActivityId(id);
        ActivityCouponVO vo = ActivityCouponVO.builder().id(domain.getId()).name(domain.getName()).title(domain.getTitle()).couponList(couponDomainList).startDate(domain.getStartDate()).endDate(domain.getEndDate()).entranceImg(domain.getEntranceImg()).internalTopImg(domain.getInternalTopImg()).remark(domain.getRemark()).build();
        return vo;
    }

    @Override
    public List<ActivityDomain> selectAll() {
        List<ActivityDomain> activityDomainList=super.list(Wrappers.<ActivityDomain>lambdaQuery().orderByDesc(ActivityDomain::getCreateTime));
        List<ActivityDomain> activityList=new ArrayList<>();
        LocalDateTime now=LocalDateTime.now();
        for(ActivityDomain domain:activityDomainList){
            Boolean isIn= CommonUtil.isInTimeLine(now,domain.getStartDate(),domain.getEndDate());
            if(!isIn){
                    if(now.isAfter(domain.getEndDate())&&domain.getStatus()!=ActivityStatus.COMPLETED.getValue()){
                        super.update(Wrappers.<ActivityDomain>lambdaUpdate().set(ActivityDomain::getStatus,ActivityStatus.COMPLETED.getValue()).eq(ActivityDomain::getId,domain.getId()));
                    }
            }else{
                ///在时间范围内&状态为【未开始】
                if(domain.getStatus()==ActivityStatus.PENDING.getValue()){
                    super.update(Wrappers.<ActivityDomain>lambdaUpdate().set(ActivityDomain::getStatus,ActivityStatus.DOING.getValue()).eq(ActivityDomain::getId,domain.getId()));
                }
            }
            activityList.add(domain);
        }
        return activityList;
    }

    @Override
    public Boolean relateCouponList(Long id, List<Long> couponIdList) {
        return baseMapper.relateCouponsById(id, couponIdList);
    }

    @Override
    public Boolean deleteById(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean unRelateCouponById(Long id, Long couponId) {
        return baseMapper.unRelateCoupon(id, couponId);
    }

    @Override
    public List<ActivityCouponVO> findByKeywords(List<String> keywords) {
        List<ActivityCouponVO> activityCouponVOList = new ArrayList<>();
        List<CouponDomain> couponDomainList;
        LocalDateTime now=LocalDateTime.now();
        for (String keyword : keywords) {
            ActivityDomain domain = super.getOne(Wrappers.<ActivityDomain>lambdaQuery().eq(ActivityDomain::getName, keyword));
            if(domain==null){
                ///返回告知前端,活动不存在
                throw BusinessException.RESULT_DATA_NONE;
            }
            Boolean isIn= CommonUtil.isInTimeLine(now,domain.getStartDate(),domain.getEndDate());
            if(isIn){
                if(domain.getStatus()==ActivityStatus.PENDING.getValue()){
                    domain.setStatus(ActivityStatus.DOING.getValue());
                }
                couponDomainList = baseMapper.findCouponByActivityId(domain.getId());
                ActivityCouponVO vo = ActivityCouponVO.builder().id(domain.getId()).name(domain.getName()).title(domain.getTitle()).couponList(couponDomainList).startDate(domain.getStartDate()).endDate(domain.getEndDate()).entranceImg(domain.getEntranceImg()).internalTopImg(domain.getInternalTopImg()).remark(domain.getRemark()).build();
                activityCouponVOList.add(vo);
            }
        }
        return activityCouponVOList;
    }

    @Override
    public Boolean updateStatusToClosedById(Long id) {
        ActivityDomain domain=super.check(id);
        if(domain.getStatus()!=ActivityStatus.DOING.getValue()){
            ///非进行中的活动
            throw BusinessException.FAILURE;
        }
        return super.update(Wrappers.<ActivityDomain>lambdaUpdate().eq(ActivityDomain::getId, id).set(ActivityDomain::getStatus,ActivityStatus.CLOSED.getValue()));
    }

    @Override
    public Boolean updateStatusToDoingById(Long id) {
        ActivityDomain domain=super.check(id);
        LocalDateTime now=LocalDateTime.now();
        Boolean isIn= CommonUtil.isInTimeLine(now,domain.getStartDate(),domain.getEndDate());
        if(!isIn){
            ///不在活动时间内
            throw BusinessException.FAILURE;
        }
        return super.update(Wrappers.<ActivityDomain>lambdaUpdate().eq(ActivityDomain::getId, id).set(ActivityDomain::getStatus,ActivityStatus.DOING.getValue()));
    }

}
